﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class SecurityUserDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SecurityUserDAL()
        {
        }
        public bool CheckPassword(string Sequence, string OldPassword)
        {
            string strSQL;
            bool bOK = false;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@OldPassword", OldPassword);

                DataSet ds = null;
                DataRowCollection drc;

                strSQL = "SELECT *"
                    + " FROM SYS_SC_USERS"
                    + " WHERE USER_CODE = @Sequence"
                        + " AND USER_PASSWORD = @OldPassword";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                if (ds.Tables.Count > 0)
                {
                    drc = ds.Tables[0].Rows;
                    bOK = drc.Count > 0;
                }
                return bOK;
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }

        }
        public DataSet GetUserInfo(Nullable<int> Sequence, string UserName)
        {
            string strSQL;
            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@UserName", UserName);
                dbManager.AddParameters(2, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT u.*, e.working_job_site_seq"
                    + " FROM SYS_SC_USERS u"
                    + " left join hrm_ps_employees e on u.emp_seq = e.emp_seq"
                    + " WHERE u.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND u.USER_SEQ = @Sequence" : "")
                    + (string.IsNullOrEmpty(UserName) ? "" : " AND u.USER_CODE = @UserName");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                return ds;
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }

        }
    }
}
